<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22 0022
 * Time: 10:10
 */

namespace app\admin\controller;


use think\Controller;
use think\Config;
use think\Loader;
use think\Db;

class UserController extends BaseController
{
    //主页
    public function index(){
        if (request()->isAjax()){
            $page=input("param.page");
            $limit=input("param.limit",100);
            $sum=($page-1)*$limit;
            $keyword=input("param.keyword");
            $where=[];
            $where="status in (1,2)";
            if ($keyword){
                $where="user_name like '%{$keyword}%'";
            }
            $data=Db::name("user")->where($where)->order("id",'desc')->limit("$sum,$limit")->select();
            $num=Db::name("user")->where($where)->order("id",'desc')->select();
            $list["code"]=0;
            $list["msg"]="获取成功";
            $list["count"]=count($num);
            $list["data"]=$data;
            return json($list);

        }
        return $this->fetch("index");
    }
    /*
     * 会员的添加
     */
    public function add(){
        if (request()->isPost()){
            $list["user_name"]=input("param.user_name");
            $list["password"]=pswCrypt(input("param.password"));
            $list["email"]=input("param.email");
            $list["phone"]=input("param.phone");
            $list["status"]=input("param.status");
            $list["age"]=input("param.age");
            $list["user_logo"]=input("param.user_logo");
            $list["last_ip"]=\request()->ip();
            $list["add_time"]=time();
            $list["user_system"]=get_os();
            $data=Db::name("user")->insert($list);
            if ($data){
                return json(["code"=>1,"msg"=>"添加成功"]);
            }else{
                return json(["code"=>0,"msg"=>"添加失败"]);
            }

        }
        return $this->fetch("add");
    }

    /*
     * 会员的编辑
     */
    public function edit(){
        $id=input("param.id");
        if (request()->isPost()){
            $list["user_name"]=input("param.user_name");
            $list["password"]=pswCrypt(input("param.password"));
            $list["email"]=input("param.email");
            $list["phone"]=input("param.phone");
            $list["status"]=input("param.status");
            $list["age"]=input("param.age");
            $list["user_logo"]=input("param.user_logo");
            $list["last_ip"]=\request()->ip();
            $list["add_time"]=time();
            $list["update_time"]=time();
            $id=input("param.id");
            $list["user_system"]=get_os();
            $data=Db::name("user")->where(["id"=>$id])->update($list);
            if ($data){
                return json(["code"=>1,"msg"=>"编辑成功"]);
            }else{
                return json(["code"=>0,"msg"=>"编辑失败"]);
            }
        }

        $find=Db::name("user")->where(["id"=>$id])->find();
        $this->assign("find",$find);
        return $this->fetch("edit");
    }

    public function del(){
        $id=input("param.id");
        $ids=input("ids/a");
        if ($ids){
            writelog("批量删除会员");
            $ids=implode(",",$ids);
            $result=Db::name("user")->where("id in ($ids)")->delete();

        }else{
            writelog("删除会员");
            $result=Db::name("user")->where(["id"=>$id])->delete();
        }

        if ($result){
            return json(["code"=>1,"msg"=>"删除成功"]);
        }else{
            return json(["code"=>0,"msg"=>"删除失败"]);
        }
    }


}